package com.tiger.cloud.service.crm;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tiger.cloud.common.exception.ServiceException;
import com.tiger.cloud.common.mybatis.MybatisTigerDao;
import com.tiger.cloud.common.mybatis.TigerBaseService;
import com.tiger.cloud.common.view.SpringUtils;
import com.tiger.cloud.dao.crm.LoanRecordDao;
import com.tiger.cloud.enums.CommucationCustomerType;
import com.tiger.cloud.enums.LoanRecordRowType;
import com.tiger.cloud.enums.PointCodeEnum;
import com.tiger.cloud.form.CommucationMessage;
import com.tiger.cloud.model.crm.Customer;
import com.tiger.cloud.model.crm.LoanRecord;
import com.tiger.cloud.model.crm.SystemCommucation;
import com.tiger.cloud.model.crm.UserInfo;
import com.tiger.cloud.model.share.PointCodeMessage;
import com.tiger.cloud.util.DateTimeUtil;
import com.tiger.cloud.util.ErrorCode;

/**
 * 合作记录/借款记录
 */
@Service("loanRecordService")
public class LoanRecordService extends TigerBaseService<LoanRecord> implements ILoanRecordService{
    
    @Autowired
	private LoanRecordDao loanRecordDao;

	@Override
	public MybatisTigerDao<LoanRecord> getEntityDao() {
		return loanRecordDao;
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean insertLoanRecord(LoanRecord loanRecord) {
		if(loanRecordDao.insert(loanRecord)>0){
			SystemCommucation systemCommucation = new SystemCommucation();
			systemCommucation.setCustomerId(loanRecord.getCustomerId());
			systemCommucation.setCustomerType(loanRecord.getType()==LoanRecordRowType.COOPERATIVE_RECORD?CommucationCustomerType.PEERS:CommucationCustomerType.CUSTOMER);
			systemCommucation.setUserId(loanRecord.getUserId());
			if(loanRecord.getType()==LoanRecordRowType.COOPERATIVE_RECORD){//合作记录
				UserInfo userInfo = SpringUtils.getBean(IUserInfoService.class).findOne(new UserInfo(){
					private static final long serialVersionUID = 1L;
				{
					setUserId(loanRecord.getCustomerId());
				}});
				systemCommucation.setContent(CommucationMessage.getMsg(CommucationMessage.ADD_LOAN_RECORD_JOINT_WORK,userInfo.getUserName(),loanRecord.getCustomerName()
						,String.valueOf(loanRecord.getAmount()),loanRecord.getLoanPeriod(),DateTimeUtil.formatDatetoString(loanRecord.getStartDate())
				));
			}else if(loanRecord.getType()==LoanRecordRowType.LOAN){//借款记录
				 Customer custormer= SpringUtils.getBean(ICustomerService.class).findOne(new Customer(){
					private static final long serialVersionUID = 1L;
				{
					setId(loanRecord.getCustomerId());
				}});
				systemCommucation.setContent(CommucationMessage.getMsg(CommucationMessage.ADD_LOAN_RECORD_OFFER_LOAN,custormer.getCustName(),String.valueOf(loanRecord.getAmount()),loanRecord.getLoanPeriod(),DateTimeUtil.formatDatetoString(loanRecord.getStartDate())
				));
			}
			if (SpringUtils.getBean(ICommucationService.class).insertSystemCommucation(systemCommucation)) {
				if(loanRecord.getType()==LoanRecordRowType.LOAN){//借款记录
					if(SpringUtils.getBean(IUserConsumeService.class).insertRecord(PointCodeEnum.ADD_MUTI_COMMUCATION.getCode(), loanRecord.getUserId(), 
							PointCodeMessage.getMsg(PointCodeMessage.ADD_MUTI_COMMUCATION,CommonPointRule.getPoint(PointCodeEnum.ADD_MUTI_COMMUCATION.getCode()))) !=null){
						return true;
					}
				}
				return true;
			}
		}
		throw new ServiceException(ErrorCode.INSERT_DATA_ERROR);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean updateLoanRecord(LoanRecord loanRecord) {
		return loanRecordDao.update(loanRecord)>0;
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean deleteLoanRecord(LoanRecord loanRecord) {
		if( loanRecordDao.update(loanRecord)>0){
			return true;
		};
		throw new ServiceException(ErrorCode.REMOVE_DATE_ERROR);
	}
	
	
    
}